package ar.com.pragmatia.lanacion.data;

import java.util.Vector;

import android.content.ContentValues;
import ar.com.pragmatia.lanacion.app.App;
import ar.com.pragmatia.lanacion.utils.Strings;

public class OrderDetail extends AppData implements Datable {

	public static final String TABLE_NAME = "order_details";
	public static final String ID = "";
	public static final String FIELDS[] = { "id", "order_id", "article_id",
			"quantity", "price", "created", "modified", "sent" };

	private String orderId;
	private String articleId;
	private String quantity;
	private String price;
	private String created;
	private String modified;
	private String sent;
	private Article article;

	public OrderDetail() {
		super();
		sent = "false";
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getArticleId() {
		return articleId;
	}

	public void setArticleId(String articleId) {
		this.articleId = articleId;
	}

	public String getQuantity() {
		return quantity;
	}

	public void setQuantity(String quantity) {
		this.quantity = quantity;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public String getCreated() {
		return created;
	}

	public void setCreated(String created) {
		this.created = created;
	}

	public String getModified() {
		return modified;
	}

	public void setModified(String modified) {
		this.modified = modified;
	}

	public Article getArticle() {
		if (article == null) {
			article = (Article) new Article().find(
					"WHERE id = '" + getArticleId() + "'").get(0);
		}
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public boolean save(boolean flag) {

		if (flag) {
			setId(getInsertId());
			ContentValues values = new ContentValues();

			for (int i = 0; i < FIELDS.length; i++) {

				String value = null;
				try {
					value = executeStringGetterMethod(this.getClass(), "get"
							+ Strings.camelize(FIELDS[i], false));
				} catch (NoSuchMethodException e) {
					// e.printStackTrace();
					// App.log("AppData", "save", this.getClass().getName());
					App.log("AppData", "save", e.getMessage());
				}

				if (FIELDS[i].equals("id") && Strings.isNullOrEmpty(value)) {
					continue;
				} else {
					value = (Strings.isNullOrEmpty(value)) ? "" : value;
					values.put(FIELDS[i], value);
				}

			}

			long r = -1;
			try {
				r = App.getDb().insert(getTableName(), null, values);
			} catch (Exception e) {
				e.printStackTrace();
				App.log("AppData", "save", e.getMessage());
			}
			return (r >= 0) ? true : false;
		}
		return false;
	}

	private String getInsertId() {
		String id = "10000";
		Vector<AppData> v = this.find("WHERE sent = 'false' ORDER BY id DESC");
		if (v.size() > 0) {
			id = (new Integer(v.get(0).getId()) + 1) + "";
		}
		return id;
	}

	public String getSent() {
		return sent;
	}

	public void setSent(String sent) {
		this.sent = sent;
	}

	@Override
	public String toString() {
		return "OrderDetail [orderId=" + orderId + ", articleId=" + articleId
				+ ", quantity=" + quantity + ", price=" + price + ", created="
				+ created + ", modified=" + modified + ", sent=" + sent
				+ ", article=" + article + "]";
	}

}
